package example.tallydemo.MyEntity;

import android.content.Context;

import org.greenrobot.greendao.query.WhereCondition;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import example.tallydemo.MyTools.MyTimeTool;
import example.tallydemo.greendao.AccountBookDao;
import example.tallydemo.greendao.DaoMaster;
import example.tallydemo.greendao.DaoSession;

/**
 * Created by tuzha on 2018/4/9.
 */

public class AccountManager implements Serializable
{
    public DaoMaster.DevOpenHelper mDevOpenHelper;
    public DaoMaster mDaoMaster;
    public DaoSession mDaoSession;
    public AccountBookDao mAccountBookDao;
    public Context context;
    ArrayList<AccountBook> books = new ArrayList<>();

    public AccountManager(Context context)
    {
        this.context = context;
    }

    public void load()
    {
        mDevOpenHelper = new DaoMaster.DevOpenHelper(context, "person.db", null);
        mDaoMaster = new DaoMaster(mDevOpenHelper.getWritableDb());
        mDaoSession = mDaoMaster.newSession();
        mAccountBookDao = mDaoSession.getAccountBookDao();
    }

    //插入一条数据
    public void insert(AccountBook accountBook)
    {
        mAccountBookDao.insert(accountBook);
    }

    //删除一条数据
    public void delete(AccountBook accountBook)
    {
        mAccountBookDao.delete(accountBook);
    }

    //更新一条数据
    public void update(AccountBook accountBook)
    {
        mAccountBookDao.insertOrReplace(accountBook);
    }

    //获取全部的账本
    public List<AccountBook> query()
    {
        List<AccountBook> books = mAccountBookDao
                .queryBuilder().list();
        return books;
    }

    public List<AccountBook> queryTime(Date date1, Date date2)
    {
        MyTimeTool myTimeTool = new MyTimeTool(date1);
        MyTimeTool myTimeTool2 = new MyTimeTool(date2);

        WhereCondition a,b,c,i,j,k;
        a = AccountBookDao.Properties.Year.ge(myTimeTool.getYear());
        b = AccountBookDao.Properties.Mouth.ge(myTimeTool.getMonth());
        c = AccountBookDao.Properties.Day.ge(myTimeTool.getDay());

        i = AccountBookDao.Properties.Year.le(myTimeTool2.getYear());
        j = AccountBookDao.Properties.Mouth.le(myTimeTool2.getMonth());
        k = AccountBookDao.Properties.Day.le(myTimeTool2.getDay());


        List<AccountBook> books = mAccountBookDao.queryBuilder()
                .where(a,b,c,i,j,k).list();
        return books;
    }

    public List<AccountBook> queryMouth(int year, int mouth)
    {
        WhereCondition i, j;
        i = AccountBookDao.Properties.Year.eq(year);
        j = AccountBookDao.Properties.Mouth.eq(mouth);
        List<AccountBook> books = mAccountBookDao
                .queryBuilder().where(i, j).list();
        return books;
    }

    public List<AccountBook> queryDate(int year, int mouth, int day)
    {
        WhereCondition i, j, k;
        i = AccountBookDao.Properties.Year.eq(year);
        j = AccountBookDao.Properties.Mouth.eq(mouth);
        k = AccountBookDao.Properties.Day.eq(day);
        List<AccountBook> books = mAccountBookDao
                .queryBuilder().where(i, j, k).list();
        return books;
    }


    public AccountBook getItemFromID(Long id)
    {
        AccountBook book = mAccountBookDao.queryBuilder()
                .where(AccountBookDao.Properties.Id.eq(id)).unique();
        return book;
    }

    public String[] setDate(Date date)
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String str = df.format(date);
        String str_date[] = str.split("-");
        return str_date;
    }

}


